package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.constant.MachineType;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.constant.SmsModelTypeConstant;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.RuleRideLog;
import com.tbit.uqbike.object.pojo.RuleRidePhotoLog;
import com.tbit.uqbike.object.pojo.Usermsg;
import com.tbit.uqbike.object.pojo.vo.RuleRideLogVo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.RuleRideLogService;
import com.tbit.uqbike.service.rpcservice.SmsModelService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.UsermsgDao;
import com.tbit.uqbike.webmanager.dao.log.RuleRideLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:18 2022/7/15
 */
@Service
public class RuleRideLogServiceImpl implements RuleRideLogService {

    @Autowired
    private RuleRideLogDao ruleRideLogDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private UsermsgDao usermsgDao;
    @Autowired
    private SmsModelService smsModelService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountDao accountDao;

    @Override
    public PageResult<RuleRideLogVo> queryPage(String phone, String startTime, String endTime,
                                               Integer state, Integer pageNO, Integer rowCount, Integer mapType,Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        List<Account> accountS = accountDao.getById(accountId);
        List<Integer> accountIdS = accountS.stream().map(Account::getAccountId).collect(Collectors.toList());
        map.put("accountIdS", accountIdS);
        map.put("phone", phone);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("state", state);
        //map.put("pageNO", rowCount * (pageNO - 1));
        //map.put("rowCount", rowCount);
        //List<RuleRideLogVo> ruleRideLogVos = ruleRideLogDao.queryPage(map);
        PageInfo<RuleRideLogVo> pageInfo = PageHelper.startPage(pageNO, rowCount).doSelectPageInfo(() -> ruleRideLogDao.queryPage(map));
        /**经纬度校准*/
        List<RuleRideLogVo> ruleRideLogVos = pageInfo.getList();
        for (RuleRideLogVo r : ruleRideLogVos) {
            Point pointDB = new Point(r.getLon(), r.getLat());
            Point point = calibrateService.commonCalibrate(pointDB, MapConstant.MAP_ORI, mapType);
            r.setLonC(point.getX());
            r.setLatC(point.getY());
        }
        return new PageResult<>(pageNO, rowCount, pageInfo.getTotal(), pageInfo.getPages(), ruleRideLogVos);
    }

    @Override
    public Integer queryCount(String phone, String startTime, String endTime, Integer state) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("endTime", endTime);
        map.put("state", state);
        map.put("startTime", startTime);
        return ruleRideLogDao.queryCount(map);
    }

    @Override
    public void edit(Integer rspRet, String rspMsg, Integer rideId, Integer sendSms) {
        RuleRideLog ruleRideLogDB = ruleRideLogDao.getByRideId(rideId);
        if (ruleRideLogDB == null) {
            return;
        }
        /**查询品牌id*/
        Integer brandId = accountService.getBrandId(ruleRideLogDB.getAccountId());
        RuleRideLog ruleRideLog = new RuleRideLog();
        ruleRideLog.setRideId(rideId);
        ruleRideLog.setRspMsg(rspMsg);
        ruleRideLog.setRspRet(rspRet);
        ruleRideLog.setHandelTime(new Date());
        /**状态 0未申诉、1申述中、2已处理*/
        ruleRideLog.setState(2);
        ruleRideLogDao.edit(ruleRideLog);
        /**下发短信提醒*/
        if (sendSms == 1) {
            Map<String, String> param = new HashMap<>();
            param.put("time", DateUtil.now());
            if (MachineType.BUSINESS_EVENT_TYPE_RETROGRADE.equals(ruleRideLogDB.getType())) {
                param.put("type", "逆行");
            } else if (MachineType.BUSINESS_EVENT_TYPE_RUN_MOTORWAY.equals(ruleRideLogDB.getType())) {
                param.put("type", "走机动车道");
            } else {
                param.put("type", "闯红灯");
            }
            Integer type;
            if (rspRet == 1) {
                type = SmsModelTypeConstant.SMS_RULE_RIDE_SUCCESS;
            } else {
                type = SmsModelTypeConstant.SMS_RULE_RIDE_FAIL;
            }
            smsModelService.sendSmsByModel(brandId, ruleRideLogDB.getPhone(),
                    type, param);
            Usermsg usermsg = new Usermsg();
            usermsg.setMsgtype(0);
            usermsg.setUserid(ruleRideLogDB.getUserId());
            usermsg.setCreatetime(new Date());
            usermsg.setMsgstate(0);
            usermsg.setTitle("非文明骑行记录申述处理通知");
            usermsg.setContent(rspMsg);
            usermsg.setRemark("系统生成");
            usermsgDao.insertSelective(usermsg);
        }
    }


    @Override
    public List<RuleRidePhotoLog> getPhoto(Integer rideId) {
        return ruleRideLogDao.getPhoto(rideId);
    }

    @Override
    public RuleRidePhotoLog getByFtpUrl(String ftpUrl) {
        return ruleRideLogDao.getByFtpUrl(ftpUrl);
    }
}
